#!/usr/bin/env python3
"""
读取cdf.txt文件，计算累积百分比，并从分布中采样16个数据点
"""
import os
from collections import defaultdict

def analyze_cdf_file():
    cdf_file = "cdf.txt"
    if not os.path.exists(cdf_file):
        print(f"文件不存在: {cdf_file}")
        return

    length_counts = defaultdict(int)
    total_count = 0

    print(f"读取文件: {cdf_file}")
    try:
        with open(cdf_file, 'r', encoding='utf-8') as f:
            for line in f:
                line = line.strip()
                if line:
                    # 处理CSV格式 (逗号分隔) 或空格分隔
                    if ',' in line:
                        parts = line.split(',')
                    else:
                        parts = line.split()
                    
                    if len(parts) >= 2:
                        # 跳过头部行
                        if parts[0].lower() in ['generate_length', 'length']:
                            continue
                        try:
                            length = int(parts[0])
                            count = int(parts[1])
                            length_counts[length] = count
                            total_count += count
                        except ValueError:
                            # 跳过无法解析的行
                            continue
    except Exception as e:
        print(f"读取文件时出错: {e}")
        return

    print(f"总count数: {total_count}")
    print(f"不同长度数: {len(length_counts)}")

    # 按长度排序
    sorted_lengths = sorted(length_counts.keys())
    
    # 计算累积百分比
    cumulative_count = 0
    cumulative_percentages = []

    print("\n" + "=" * 60)
    print("累积百分比分析:")
    print("=" * 60)
    print(f"{'长度':<8} {'Count数':<10} {'累积Count':<12} {'累积百分比':<12}")
    print("-" * 60)

    for length in sorted_lengths:
        count = length_counts[length]
        cumulative_count += count
        percentage = (cumulative_count / total_count) * 100
        
        cumulative_percentages.append({
            'length': length,
            'count': count,
            'cumulative_count': cumulative_count,
            'cumulative_percentage': percentage
        })
        
        print(f"{length:<8} {count:<10} {cumulative_count:<12} {percentage:.2f}%")

    # 保存分析结果
    result_file = "cumulative_analysis.txt"
    with open(result_file, 'w', encoding='utf-8') as f:
        f.write("CDF累积百分比分析\n")
        f.write("=" * 60 + "\n\n")
        f.write(f"总count数: {total_count}\n")
        f.write(f"不同长度数: {len(length_counts)}\n\n")
        
        f.write("累积百分比详情:\n")
        f.write("-" * 60 + "\n")
        f.write(f"{'长度':<8} {'Count数':<10} {'累积Count':<12} {'累积百分比':<12}\n")
        f.write("-" * 60 + "\n")
        
        for item in cumulative_percentages:
            f.write(f"{item['length']:<8} {item['count']:<10} {item['cumulative_count']:<12} {item['cumulative_percentage']:.2f}%\n")
        
        # 添加一些关键百分比的统计
        f.write("\n" + "=" * 60 + "\n")
        f.write("关键百分比统计:\n")
        f.write("-" * 30 + "\n")
        
        # 找到达到50%、80%、90%、95%的长度
        key_percentages = [50, 80, 90, 95]
        for target_pct in key_percentages:
            for item in cumulative_percentages:
                if item['cumulative_percentage'] >= target_pct:
                    f.write(f"达到{target_pct}%的长度: {item['length']} (累积{target_pct:.1f}%)\n")
                    break

    print(f"\n分析结果已保存到: {result_file}")
    
    # 采样16个数据点，保持分布
    sampled_data = sample_from_cdf(length_counts, total_count, 16)
    
    # 保存采样结果
    sample_file = "sampled_16_points.txt"
    with open(sample_file, 'w', encoding='utf-8') as f:
        f.write("从CDF分布中采样的16个数据点\n")
        f.write("=" * 40 + "\n\n")
        f.write("采样结果:\n")
        for i, length in enumerate(sampled_data, 1):
            f.write(f"点{i:2d}: 长度 {length}\n")
    
    print(f"\n采样结果已保存到: {sample_file}")
    print("采样的16个数据点:")
    for i, length in enumerate(sampled_data, 1):
        print(f"点{i:2d}: 长度 {length}")
    
    return cumulative_percentages, sampled_data

def sample_from_cdf(length_counts, total_count, num_samples):
    """
    从CDF分布中采样指定数量的数据点
    """
    import random
    
    # 按长度排序
    sorted_lengths = sorted(length_counts.keys())
    
    # 构建累积分布
    cumulative_dist = []
    cumulative_count = 0
    
    for length in sorted_lengths:
        count = length_counts[length]
        cumulative_count += count
        cumulative_dist.append((length, cumulative_count))
    
    # 采样
    sampled_data = []
    for _ in range(num_samples):
        # 生成0到total_count之间的随机数
        rand_val = random.uniform(0, total_count)
        
        # 找到对应的长度
        for length, cum_count in cumulative_dist:
            if rand_val <= cum_count:
                sampled_data.append(length)
                break
    
    return sampled_data

if __name__ == "__main__":
    results = analyze_cdf_file() 